package com.example.demo.col.controller;

import com.example.demo.col.HelloService;
import com.example.demo.col.MySingletonBean;
import com.example.demo.col.UserDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
public class Controller {
    int i = 0;
    @Resource
    HelloService helloService;
    @Resource
    ApplicationContext applicationContext;

    @GetMapping("hello")
    public String hello() {
         i+=1;
        MySingletonBean bean1 = applicationContext.getBean(MySingletonBean.class);
        MySingletonBean bean2 = applicationContext.getBean(MySingletonBean.class);

        // 验证两个实例是否相等
        return String.format("Bean1 ID: %s\nBean2 ID: %s\nIs Same Instance: %b number %d",
                bean1.getId(), bean2.getId(), bean1 == bean2, i);
    }
    @PostMapping("user")
    public String getUser(@RequestBody UserDto userDto) {
        return String.format("name: %s", userDto.getName());
    }
}
